package com.funcourse.demo.util;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ReflectionUtil {
	
	private static final Logger LOGGER=LoggerFactory.getLogger(ReflectionUtil.class); 
	
	public static String[] getNullPropertyNames(Object source) {
		Set<String> emptyNames = new HashSet<String>();
		Field[] declaredFields = source.getClass().getDeclaredFields();
		for(Field field : declaredFields) {
			field.setAccessible(true);
			try {
				if(null == field.get(source)) {
					emptyNames.add(field.getName());
				}
			} catch (IllegalArgumentException | IllegalAccessException e) {
				LOGGER.warn("Error occuring during retriving null fields of {}",source,e);
			}
		}
		String[] result = new String[emptyNames.size()];
		return emptyNames.toArray(result);
	}
}
